package com.wusuowei.system.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.wusuowei.common.annotation.Log;
import com.wusuowei.common.constant.BusinessTypeEnum;
import com.wusuowei.common.utils.R;
import com.wusuowei.system.mapper.SysFileMapper;
import com.wusuowei.system.model.po.SysFile;
import com.wusuowei.system.model.vo.FileUploadInfo;
import com.wusuowei.system.model.vo.PageFileVo;
import com.wusuowei.system.service.ISysFileService;
import com.wusuowei.system.utils.MinioUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lgy
 */
@Slf4j
@PreAuthorize("isAuthenticated()")
@RestController
@RequestMapping("file")
public class SysFileController {

    @Autowired
    private SysFileMapper fileMapper;
    @Autowired
    private MinioUtils minioUtils;
    @Autowired
    private ISysFileService sysFileService;


    @PreAuthorize("hasAuthority('system:file:list')")
    @GetMapping("/page")
    public R findPage(PageFileVo pagequery) {
        List<SysFile> page = sysFileService.findPage(pagequery);
        HashMap<String, Object> map = new HashMap<>();
        List<SysFile> records = CollectionUtil.sub(page, (pagequery.getPageNum() - 1) * pagequery.getPageSize(), pagequery.getPageNum() * pagequery.getPageSize());
        map.put("records",records);
        map.put("total",page.size());
        return R.ok().setData(map);
    }

    @PreAuthorize("hasAuthority('system:file:edit')")
    @Log(title = "文件管理",businessType = BusinessTypeEnum.UPDATE)
    @PostMapping("/update")
    public R update(@RequestBody SysFile files) {
        return R.ok().setData(fileMapper.updateById(files));
    }

    @PreAuthorize("hasAuthority('system:file:remove')")
    @Log(title = "文件管理",businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping()
    public R delete(@RequestBody FileUploadInfo fileUploadInfo) {
        try {
            minioUtils.deleteObject(fileUploadInfo.getFileType(), fileUploadInfo.getFileName());
        } catch (Exception e) {
            return R.error("文件删除失败");
        }
        fileMapper.deleteById(fileUploadInfo.getId());
        return R.ok();
    }

    @PreAuthorize("hasAuthority('system:file:remove')")
    @Log(title = "文件管理",businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/del/batch")
    public R deleteBatch(@RequestBody List<FileUploadInfo> fileUploadInfos) {
        // select * from sys_file where id in (id,id,id...)
        List<Integer> collect = fileUploadInfos.stream().map(FileUploadInfo::getId).collect(Collectors.toList());
        for (FileUploadInfo fileUploadInfo : fileUploadInfos) {
            try {
                minioUtils.deleteObject(fileUploadInfo.getFileType(), fileUploadInfo.getFileName());
            } catch (Exception e) {
                return R.error("文件删除失败");
            }
        }
        fileMapper.deleteBatchIds(collect);
        return R.ok();
    }

}
